﻿// Copyright (c) 2012 - 2020 Jared Zheng <jaredz at outlook dot com>
// Buddha Framework is licensed under Mulan PSL v2.
// You can use this software according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of Mulan PSL v2 at:
//          http://license.coscl.org.cn/MulanPSL2
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
// See the Mulan PSL v2 for more details.
//
// Create : 2012-12-01 version 0.1 Jared Zheng <jaredz at outlook dot com>
// Update : 2018-12-05 version 3.1.5 Jared Zheng <jaredz at outlook dot com>

#include "stdafx.h"
#include "ServerConsole.h"
#include "AuthorDBHandler.h"

///////////////////////////////////////////////////////////////////////////////////////////////////////////
// CAuthorDBHandler
CAuthorDBHandler::CAuthorDBHandler(void)
: m_uAuthorDBServer(0)
, m_uAuthorDBQuery(0)
, m_uAuthorDBLoad(0)
, m_uAuthorOnline(0)
, m_uAuthorTotal(0)
{
}

CAuthorDBHandler::~CAuthorDBHandler(void)
{
}

void CAuthorDBHandler::OnLive(bool bStart)
{
    CStringFix strTemp = GetName();
    if (bStart)
    {
        strTemp += TF("Start---");

        CStrAddr strAddr;
        GServerConsoleInst->m_Config.GetServerAddr(CServerConfig::CFG_LOAD_AUTHORDB, CServerConfig::CFG_LOAD_NONE, strAddr);
        strTemp.AppendFormat(TF("TCP Int-Listen=[%s]:%d"), *strAddr.strIp, strAddr.usPort);
    }
    else
    {
        strTemp += TF("Stop");

        m_uAuthorDBServer = 0;
        m_uAuthorDBQuery  = 0;
        m_uAuthorDBLoad   = 0;
        m_uAuthorOnline   = 0;
        m_uAuthorTotal    = 0;
    }
    LOG_INFO(GServerConsoleInst->m_FileLog, *strTemp);
}

void CAuthorDBHandler::OnLink(Int nServerIndex, uintptr_t utData)
{
    if (nServerIndex == PAKT_CENTER)
    {
        CStringFix strTemp = GetName();
        strTemp += TF("TCP Int-Connect Center=");

        CNetAddr* pAddr = (reinterpret_cast<CNetAddr*>(utData));

        CStrAddr   strAddr;
        GServerConsoleInst->m_Config.GetServerAddr(CServerConfig::CFG_LOAD_CENTER, CServerConfig::CFG_LOAD_NONE, strAddr);
        strTemp.AppendFormat(TF("[%s]:%d"), *strAddr.strIp, strAddr.usPort);

        GServerConsoleInst->m_NetworkPtr->TranslateAddr(strAddr.strIp, strAddr.usPort, *pAddr, false);
        strTemp.AppendFormat(TF("([%s]:%d)"), *strAddr.strIp, strAddr.usPort);

        LOG_INFO(GServerConsoleInst->m_FileLog, *strTemp);
    }
    else
    {
        AddServerData(nServerIndex, utData);
    }
}

void CAuthorDBHandler::OnUpdate(Int nServerIndex, uintptr_t utData)
{
    UpdateServerData(nServerIndex, utData);
}

void CAuthorDBHandler::OnSync(Int nServerIndex, uintptr_t utData)
{
    if (nServerIndex == PAKT_AUTHOR)
    {
        PSERVER_STATUS pStatus = reinterpret_cast<PSERVER_STATUS>(utData);
        m_uAuthorTotal    = pStatus->uTotal;
        m_uAuthorOnline   = pStatus->uOnline;
    }
    else
    {
        PSERVER_STATUS pStatus = reinterpret_cast<PSERVER_STATUS>(utData);
        m_uAuthorDBServer = pStatus->uTotal;
        m_uAuthorDBQuery  = pStatus->uOnline;
        m_uAuthorDBLoad   = pStatus->usLoad / DATAD_TOPERCENT;
    }
}

void CAuthorDBHandler::OnSync(Int, CStream&)
{
}

void CAuthorDBHandler::OnUnlink(Int nServerIndex, uintptr_t utData)
{
    if (nServerIndex == PAKT_CENTER)
    {
        LOG_INFO(GServerConsoleInst->m_FileLog, TF("AuthorDB:TCP Int-Connect Center Closed"));
    }
    else
    {
        RemoveServerData(nServerIndex, utData);
    }
}

void CAuthorDBHandler::UpdateServerInfo(void)
{
    CStringFix strTemp = GetName();
    strTemp.Format(TF("[Servers=%u/%u, Load=%d%%][Author:%u/%u]"),
                   m_uAuthorDBServer, m_uAuthorDBQuery, m_uAuthorDBLoad,
                   m_uAuthorOnline, m_uAuthorTotal);
    LOG_INFO(GServerConsoleInst->m_FileLog, *strTemp);
}

PCXStr CAuthorDBHandler::GetName(void)
{
    return TF("AuthorDB:");
}




